package com.lemon.smartsafetynew.ui.activity.ledger;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.smartsafetynew.entity.common.DateTypeEnum;
import com.lemon.smartsafetynew.entity.ledger.LedgerBuildingCheckItemDetailVo;
import com.lemon.smartsafetynew.entity.ledger.LedgerBuildingCheckItemVo;
import com.lemon.smartsafetynew.entity.ledger.LedgerBuildingItemVo;
import com.lemon.smartsafetynew.net.services.SafeLedgerServices;
import com.lemon.smartsafetynew.ui.activity.patrol.SafePatrolHistoryNewActivity;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.constants.TimeFormatConstants;
import com.lemon.smartsafetynew.util.patrol.CheckItemUtil;
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Andrew on 2017/9/5.
 */

public class LedgerBuildingCheckItemDetailActivity extends SwipeBackActivity {

    @Bind(R.id.check_item_tv)
    TextView checkItemTv;
    @Bind(R.id.check_item_location_tv)
    TextView checkItemLocationTv;
    @Bind(R.id.status_image)
    ImageView statusImage;
    @Bind(R.id.status_tv)
    TextView statusTv;
    @Bind(R.id.date_tv)
    TextView dateTv;
    @Bind(R.id.total_task_num)
    TextView totalTaskNum;
    @Bind(R.id.done_task_num)
    TextView doneTaskNum;
    @Bind(R.id.to_do_task_num)
    TextView toDoTaskNum;

    @Bind(R.id.patrol_item_name)
    TextView patrolItemName;
    @Bind(R.id.patrol_location)
    TextView patrolLocation;
    @Bind(R.id.now_check_result_label)
    TextView nowCheckResultLabel;
    @Bind(R.id.now_check_result_icon)
    ImageView nowCheckResultIcon;
    @Bind(R.id.now_status)
    TextView nowStatus;
    @Bind(R.id.patrol_point)
    TextView patrolPoint;

    private static LedgerBuildingCheckItemVo ledgerBuildingCheckItemVo;
    private static LedgerBuildingItemVo ledgerBuildingItemVo;


    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {

            if (bundle.containsKey(KeyConstants.KEY_LEDGER_PATROL_CHECK_ITEM_VO)) {
                ledgerBuildingCheckItemVo = (LedgerBuildingCheckItemVo) bundle.getSerializable(KeyConstants.KEY_LEDGER_PATROL_CHECK_ITEM_VO);
            }

            if (bundle.containsKey(KeyConstants.KEY_LEDGER_PATROL_BUILDING_VO)) {
                ledgerBuildingItemVo = (LedgerBuildingItemVo) bundle.getSerializable(KeyConstants.KEY_LEDGER_PATROL_BUILDING_VO);
            }
        }
    }

    @Override
    protected void initTitle() {
        this.setBarTitle("点位详情");
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(LedgerBuildingCheckItemDetailActivity.this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_ledger_building_check_item_detail;
    }

    @Override
    protected void initViews() {
        super.initViews();

        initCommonUi();
        getCheckItemDetail();
    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        setIconEnable(menu, true);
//        getMenuInflater().inflate(R.menu.menu_calendar, menu);
//        return true;
//    }
//
//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//        switch (item.getItemId()) {
//            case R.id.action_calendar: {
//                pickCheckDate();
//                break;
//            }
//        }
//        return super.onOptionsItemSelected(item);
//    }

    /**
     * 修改日期
     */
    private void pickCheckDate() {
        Calendar now = Calendar.getInstance();
        DatePickerDialog dpd = DatePickerDialog.newInstance(new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {

                Calendar calendar = Calendar.getInstance();
                calendar.set(year, monthOfYear, dayOfMonth);
                SimpleDateFormat dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_DATE, Locale.getDefault());
                String dateStr = dateFormat.format(calendar.getTime());
                dateTv.setText(dateStr);

                getCheckItemDetail();

            }
        }, now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));
        dpd.setThemeDark(false);
        dpd.vibrate(false);
        dpd.show(getFragmentManager(), Constants.DIALOG_TAG_CHECK_DATE);
    }


    /**
     * 基本UI初始化
     */
    private void initCommonUi() {

        //dateTv.setText(CommonUtil.getNowTimeStr(DateTypeEnum.DAY));

        checkItemTv.setText(CommonUtil.avoidNullString(ledgerBuildingCheckItemVo.getItem_name()));
        //checkItemLocationTv.setText(CommonUtil.avoidNullString(ledgerBuildingItemVo.getBuilding_name())
        //        + " " + CommonUtil.avoidNullString(ledgerBuildingCheckItemVo.getFloor_num())
        //        + "楼 " + CommonUtil.avoidNullString(ledgerBuildingCheckItemVo.getPosition_mark()));

        //statusImage.setImageResource(CheckItemUtil.getCheckItemStatusImgRes(ledgerBuildingCheckItemVo.getStatus()));
        //statusTv.setText(CheckItemUtil.getCheckItemStatusString(ledgerBuildingCheckItemVo.getStatus()));
    }

    /**
     * 从网络获取数据后，初始化内容页面
     */
    private void initContentUi(LedgerBuildingCheckItemDetailVo ledgerBuildingCheckItemDetailVo) {

        totalTaskNum.setText(CommonUtil.avoidNullString(ledgerBuildingCheckItemDetailVo.getCheck_num()));
        doneTaskNum.setText(CommonUtil.avoidNullString(ledgerBuildingCheckItemDetailVo.getRed_num()));
        toDoTaskNum.setText(CommonUtil.avoidNullString(ledgerBuildingCheckItemDetailVo.getRectify_num()));

        patrolItemName.setText(CommonUtil.avoidNullString(ledgerBuildingCheckItemVo.getItem_name()));
        //patrolLocation.setText(CommonUtil.avoidNullString(ledgerBuildingItemVo.getBuilding_name())
        //        + " " + CommonUtil.avoidNullString(ledgerBuildingCheckItemVo.getFloor_num())
        //        + "楼 " + CommonUtil.avoidNullString(ledgerBuildingCheckItemVo.getPosition_mark()));

        nowCheckResultLabel.setTextColor(Color.parseColor(CheckItemUtil.getCheckItemStatusColor(ledgerBuildingCheckItemDetailVo.getStatus())));
        nowCheckResultIcon.setImageResource(CheckItemUtil.getCheckItemStatusImgRes(ledgerBuildingCheckItemDetailVo.getStatus()));
        nowStatus.setText(CheckItemUtil.getCheckItemStatusString(ledgerBuildingCheckItemDetailVo.getStatus()));
        nowStatus.setTextColor(Color.parseColor(CheckItemUtil.getCheckItemStatusColor(ledgerBuildingCheckItemDetailVo.getStatus())));

        statusImage.setImageResource(CheckItemUtil.getCheckItemStatusImgRes(ledgerBuildingCheckItemDetailVo.getStatus()));
        statusTv.setText(CheckItemUtil.getCheckItemStatusString(ledgerBuildingCheckItemDetailVo.getStatus()));

        if (StringUtils.getBoolean(ledgerBuildingCheckItemDetailVo.getIsPatrol())) {
            patrolPoint.setText("是");
        } else {
            patrolPoint.setText("否");
        }
    }

    /**
     * 获取点位信息
     */
    private void getCheckItemDetail() {
        LemonRequestParams params = new LemonRequestParams("");
        params.put("type", "");
        params.put("date", "");
        params.put("item_id", ledgerBuildingCheckItemVo.getItem_id());
        SafeLedgerServices
                .init()
                .getBuildingCheckItemDetail(params);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(HttpCallbackEvent event) {

        if (KeyConstants.URL_GET_SAFE_LEDGER_BUILDING_CHECK_ITEM_DETAIL.equals(event.getTag())) { //获取安全台检查项详情
            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {
                mHUDUtil.show();
            } else if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow()) {
                mHUDUtil.hidden();
                if (Constants.Result.SUCCESS == event.getEventResult()) {
                    if (event.getObj() instanceof LedgerBuildingCheckItemDetailVo) {
                        initContentUi((LedgerBuildingCheckItemDetailVo) event.getObj());
                    }
                } else if (Constants.Result.ERROR == event.getEventResult()) {
                    mHUDUtil.showErrorTip(event.getMsg(), new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {

                        }
                    });
                }
            }
        }
    }

    @OnClick({R.id.item_more, R.id.item_history})
    public void onViewClicked(View view) {
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.item_more:
                bundle.putString(KeyConstants.KEY_SAFE_PATROL_ITEM_ID, ledgerBuildingCheckItemVo.getItem_id());
                UIHelper.showActivity(this, bundle, LedgerCheckItemListActivity.class);
                break;
            case R.id.item_history:
                bundle.putString(KeyConstants.KEY_SAFE_PATROL_ITEM_ID, ledgerBuildingCheckItemVo.getItem_id());
                UIHelper.showActivity(this, bundle, SafePatrolHistoryNewActivity.class);
                break;
        }
    }

}
